﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;
using UnityEngine.PlayerLoop;
using Yoozoo.Managers;
using Random = System.Random;

namespace Gameplay.PVE.Survivor
{
    public class SkillComponent: ComponentBase
    {
        private ActiveSkill mainSkill;
        protected override void OnInitialized()
        {
            if (unit.config.skill_id != null)
            {
                for (int i = 0; i < unit.config.skill_id.Count; i++)
                {
                    var config = BattleConfigManager.Instance.GetSkillConfig(unit.config.skill_id[i]);
                    if (config != null)
                    {
                        if (config.type == 6)
                        {
                            mainSkill = ClassManager.Get<OmniSlashSkill>();
                        }
                        else
                        {
                            mainSkill = ClassManager.Get<ActiveSkill>();
                        }
                        mainSkill.Initialize(config, unit);
                        unit.totalMp = mainSkill.config.value[0];
                        break;
                    }
                }
            }
        }
        
        public bool hasMainSkill => mainSkill != null;

        public bool IsSkillReady()
        {
            return hasMainSkill && unit.currentMp >= unit.totalMp;
        }

        public void AutoCastSkill()
        {
            if (mainSkill != null)
            {
                var target = SearchTargetHelper.GetSkillTarget(unit,mainSkill);
                if (target != null)
                {
                    mainSkill.SetTarget(target);
                    mainSkill.Start();
                    unit.target = target;
                    if (target != unit)
                    {
                        if (mainSkill.NeedSpecialAim())
                        {
                            unit.TargetForward = target.Position - unit.Position;
                        }
                        else if (mainSkill.NeedAim())
                        {
                            unit.StartAim(null,false);
                        }
                        else
                        {
                            unit.TargetForward = target.Position - unit.Position;
                            unit.EndAim();
                        }
                    }
                    else
                    {
                        unit.EndAim();
                    }
                    unit.SetMpChange(-unit.totalMp);
                }
                else
                {
                    unit.EndAim();
                    //UnityEngine.Debug.LogError(mainSkill.config.id + " Skill target is null");
                }
            }
        }
        
        protected override void OnUpdate()
        {
            if (mainSkill != null && mainSkill.JustEnd())
            {
                unit.target = null;
                unit.EndAim();
                mainSkill.Reset();
            }
        }

        public void UpdateSkill()
        {
            mainSkill?.Update();
        }

        public override void LateUpdate()
        {
            mainSkill?.LateUpdate();
        }

        public bool IsInSkill()
        {
            return mainSkill?.IsInSkill() ?? false;
        }
        
        protected override void OnDead()
        {
            base.OnDead();
            StopSkill();
        }

        public void StopSkill()
        {
            mainSkill?.End();
        }
        
        public override void OnReset()
        {
            if (mainSkill != null)
            {
                ClassManager.Free(mainSkill);
                mainSkill = null;
            }
        }
    }
}
